import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;

/**
 * @author AMPM
 * 
 *         This class is instantiated and set to run in an individual thread by
 *         the Simple TCP Server to handle the activities involved in serving a
 *         client.
 * 
 */

public class SaraClientHandlerOld implements Runnable {
	private Socket socket;
	private static final String privateKey = "ssssssss";
	private HashMap<String, String> keyMap = new HashMap<String, String>();

	public SaraClientHandlerOld(Socket socket) {
		this.socket = socket;
		keyMap.put("A", "aaaaaaaa");
		keyMap.put("B", "bbbbbbbb");

	}

	public void run() {
		try {

			// get the input and output stream from the socket
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();

			// wrap them in the ObjectInputStream and ObjectOutputStream objects
			// ObjectOutputStream oos = new ObjectOutputStream( os );
			ObjectOutputStream oos = SymmetricCrypt.encrypt(os, keyMap.get("A"));
			ObjectInputStream ois = new ObjectInputStream(is);

			// get the data transmitted from the client
			Boolean mode = (Boolean) ois.readObject();

			// If mode = true encrypted message
			// case depending on the transmitted boolean value
			if (mode) {
				long nonce = (Long) ois.readObject();
				String sender = (String) ois.readObject();
				String receiver = (String) ois.readObject();
				oos.writeObject(mode);
				oos.writeObject(nonce);
				oos.writeObject("S"); // sender
				oos.writeObject("B");
				
				System.out.println(nonce);
				System.out.println(sender);
				System.out.println(receiver);

			} else {
//				String message = (String) ois.readObject();
//				System.out.println("Received by Sara: " + message);
//				oos.writeObject(mode);
//				oos.writeObject("Message 2");
			}

			oos.flush();
			oos.close();

			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
/*	
	if (mode) {
		System.out.println("1");
		long nonce = (Long) ois.readObject();
		System.out.println("2");
		String sender = (String) ois.readObject();
		System.out.println("3");
		String receiver = (String) ois.readObject();
		System.out.println("4");
		oos = SymmetricCrypt.encrypt(os, "password");
		System.out.println("S: Received init message from " + sender);

		oos.writeObject(mode);
		oos.writeObject(nonce);
		oos.flush();

		// create and write ticket to stream
		String key = createKey();
		String ticket = createTicket(sender, key);

		oos.writeObject(key);

		oos.writeObject(SymmetricCrypt.EncryptString(ticket,
				keyMap.get(receiver)));
*/
	
}
